project(TcpServer LANGUAGES CXX)

# openssl加密
option(TCPSERVER_ENABLE_OPENSSL "Use OpenSSL" OFF)
if(TCPSERVER_ENABLE_OPENSSL)
    find_package(OpenSSL REQUIRED)
    message(STATUS "OPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR}")
    message(STATUS "OPENSSL_CRYPTO_LIBRARY=${OPENSSL_CRYPTO_LIBRARY}")
    message(STATUS "OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}")
endif()

file(GLOB SRC_FILES "*.cpp" "*.c")
# 添加openssl封装接口
if(TCPSERVER_ENABLE_OPENSSL)
    file(GLOB SSL_FILES "ssl/*.cpp" "ssl/*.c")
    list(APPEND SRC_FILES ${SSL_FILES})
endif()

foreach(FILE IN LISTS SRC_FILES)
    message(STATUS "add ${FILE} to ${PROJECT_NAME}")
endforeach()

add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rd/AsyncLogger")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rd/libevent")

add_library(TcpServer OBJECT ${SRC_FILES})

target_include_directories(TcpServer PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/3rd/libevent/include
    ${CMAKE_CURRENT_SOURCE_DIR}/3rd/libevent
    ${CMAKE_CURRENT_SOURCE_DIR}/3rd/AsyncLogger
    ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(TcpServer PUBLIC
    event_extra
    event_core
    AsyncLogger-static
)

if(WIN32)
    target_link_libraries(TcpServer PUBLIC ws2_32)
else()
    target_link_libraries(TcpServer PUBLIC 
        pthread
        event_pthreads
    )
endif()

if(TCPSERVER_ENABLE_OPENSSL)
    target_compile_definitions(TcpServer PUBLIC TCPSERVER_ENABLE_OPENSSL)
    target_include_directories(TcpServer PUBLIC ${OPENSSL_INCLUDE_DIR})
    target_include_directories(TcpServer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ssl)
    target_link_libraries(TcpServer PUBLIC event_openssl)
    target_link_libraries(TcpServer PUBLIC OpenSSL::SSL)
    target_link_libraries(TcpServer PUBLIC OpenSSL::Crypto)
endif()


